How to detect my browser version using JavaScript?
How to detect my browser version using JavaScript?
297
13-Jul-2023
Updated on 14-Jul-2023
Aryan Kumar
14-Jul-2023Sure, here is how to detect your browser version using JavaScript:
JavaScript
This code will first get the user agent string from the
navigatorobject. The user agent string is a string that identifies the browser and operating system that the user is using.Next, the code will use the
match()method to extract the browser version from the user agent string. Thematch()method takes a regular expression as an argument and returns an array of matches if the regular expression matches the string.The regular expression in this code matches the following strings:
Chrome/<version>Firefox/<version>Edge/<version>Safari/<version>Opera/<version>IE/<version>If the regular expression does not match the user agent string, the
match()method will return an empty array.Finally, the code will return the browser version if it was found. If the browser version was not found, the code will return
undefined.Here is an example of how to use this code:
JavaScript
This code will first call the
detectBrowserVersion()function to get the browser version. If the browser version is found, the code will log the browser version to the console. If the browser version is not found, the code will log a message to the console.